﻿using UnityEngine;
using DICE_GS;
using System.Collections;
using System.Collections.Generic;

public class AddShopItemUnits : MonoBehaviour {
    SHOPTYPE _currentType;
    private List<ShopItemUnit> unitList = new List<ShopItemUnit>();
    public ShopMainManager manager;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    public void RefreshItems()
    {
        SetItems(_currentType);
    }

    public void SetItems(SHOPTYPE type)
    {
        _currentType = type;
        //int currentChildCount = transform.childCount;
        foreach(ShopItemUnit unit in unitList)
        {
            Transform trans = unit.transform;
            trans.transform.localPosition = new Vector3(0f, 1500f, 0f);
            trans.transform.parent = null;
            DestroyImmediate(trans.gameObject);
        }

        unitList.Clear();

//        transform.parent.localPosition = Vector3.zero;
        Vector2 range = transform.parent.GetComponent<UIPanel>().clipOffset;
        range.x = 0f;
		transform.parent.GetComponent<UIPanel>().clipOffset = range;
		transform.parent.GetComponent<UIScrollView>().ResetPosition();
		Vector3 temp = transform.parent.localPosition;
		temp.x = 0f;
		transform.parent.localPosition = temp;

        if (type == SHOPTYPE.UNIT) return;
        
        List<CShopItem> itemList = new List<CShopItem>();
        switch (type)
        {
            case SHOPTYPE.ITEM:
                itemList = CShopItemDataManager.Instance.GetItemList(DICE_GS.SHOPTYPE.ITEM);
                break;
            case SHOPTYPE.CASH:
                itemList = CShopItemDataManager.Instance.GetItemList(DICE_GS.SHOPTYPE.CASH);
                break;
            case SHOPTYPE.STAMINA:
                itemList = CShopItemDataManager.Instance.GetItemList(DICE_GS.SHOPTYPE.ITEM);
                break;
            case SHOPTYPE.GOLD:
                itemList = CShopItemDataManager.Instance.GetItemList(DICE_GS.SHOPTYPE.GOLD);             
                break;
			case SHOPTYPE.SKILL:
                List<CShopItem> tempList = CShopItemDataManager.Instance.GetItemList(DICE_GS.SHOPTYPE.SKILL);

                for (int i = 0; i < tempList.Count; i++)
                {
                    string[] split = tempList[i].contents.Split(',');

                    CSkillData skillData = CSkillDataManager.Instance.FindSkillData(split[1].Trim());

                    if (!MyInfo.Instance.MySkillList.Contains(skillData.id))
                    {
                        itemList.Add(tempList[i]);
                    }
                }
				break;
			case SHOPTYPE.CHARACTER:
				itemList = CShopItemDataManager.Instance.GetItemList(DICE_GS.SHOPTYPE.CHARACTER);             
				foreach(int id in MyInfo.Instance.MyCharacterList)
				{
					for(int i=0; i<itemList.Count; i++)
					{
						Debug.LogError(id);
						if( id+100 == itemList[i].id )
						{
							Debug.LogError(itemList[i].id);
							itemList.RemoveAt(i);
							break;
						}
					}
				}
				break;
        }

        for (int i = 0; i < itemList.Count; i++)
        {
			ShopItemUnit unit = LTPrefabManager.InstantiateC(PrefabTypes.UI_Shop_ItemBtn, Vector3.zero, Quaternion.identity) as ShopItemUnit;
            unit.gameObject.transform.parent = transform;
            unit.transform.localScale = new Vector3(1f, 1f, 1f);
			if( type != SHOPTYPE.SKILL && type != SHOPTYPE.CHARACTER )
			{
            	unit.itemImage.spriteName = itemList[i].icon_name;
				unit.itemImage.gameObject.SetActive(true);
				unit.skillItemImage.gameObject.SetActive(false);
				unit.charItemImage.gameObject.SetActive(false);
			}
			else if( type == SHOPTYPE.SKILL )
			{
				unit.skillItemImage.spriteName = itemList[i].icon_name;
				unit.itemImage.gameObject.SetActive(false);
				unit.skillItemImage.gameObject.SetActive(true);
				unit.charItemImage.gameObject.SetActive(false);
                string[] split = itemList[i].contents.Split(',');
                CSkillData skillData = CSkillDataManager.Instance.FindSkillData(split[1]);
                unit.GetComponent<LTButtonMessage>().enabled = true;
                unit.GetComponent<LTButtonMessage>().m_MethodDeliver[0].m_EventListener = manager;
                unit.GetComponent<LTButtonMessage>().m_MethodDeliver[0].m_EventId = skillData.id;
			}
			else if( type == SHOPTYPE.CHARACTER )
			{
				int characterId = itemList[i].id % 100;
                unit.charItemImage.spriteName = itemList[i].icon_name;
				unit.itemImage.gameObject.SetActive(false);
				unit.skillItemImage.gameObject.SetActive(false);
				unit.charItemImage.gameObject.SetActive(true);
                unit.GetComponent<LTButtonMessage>().enabled = true;
                unit.GetComponent<LTButtonMessage>().m_MethodDeliver[0].m_EventListener = manager;
                unit.GetComponent<LTButtonMessage>().m_MethodDeliver[0].m_EventId = characterId;
			}
            unit.itemName.text = CStringTableManager.Instance.GetItemByName(itemList[i].title_string);
            unit.itemDescText.text = CStringTableManager.Instance.GetItemByName(itemList[i].desc_string);
			if( itemList[i].price_type.Equals("gold") )
				unit.itemCostTypeImage.spriteName = "IconGold";	
			else if( itemList[i].price_type.Equals("diamond") )
				unit.itemCostTypeImage.spriteName = "IconCash";
			else if( itemList[i].price_type.Equals("krw") )
				unit.itemCostTypeImage.enabled = false;

			int price = itemList[i].price;
			int numIndex = 5;
			while( price != 0 )
			{
				if( numIndex < 3 ) unit.itemCostTextImages[6].gameObject.SetActive(true);
				int priceNum = price % 10;
				Debug.LogError(price);
				unit.itemCostTextImages[numIndex].spriteName = "No_B"+priceNum;
				unit.itemCostTextImages[numIndex].gameObject.SetActive(true);
				price = price / 10;
				numIndex--;
			}

//			foreach(UILabel label in unit.itemCostText)
//			{
//				if( itemList[i].price_type.Equals("krw") )
//				   	label.text = itemList[i].price.ToString() + " won";
//				else
//            		label.text = itemList[i].price.ToString();
//			}

            unit.ItemId = itemList[i].id;
            unit.manager = this;
            unitList.Add(unit);
        }
        
        GetComponent<UIGrid>().Reposition();
    }
}
